//
//  Main.c
//  ARDUINO_PROPELLER
//
//  Created by FRANCIS PAPINEAU on 12-02-06.
//  Copyright 2012 carleton university. All rights reserved.
//

// Includes
#include <avr/pgmspace.h>
#include <avr/sleep.h>

// Mode Select
#define RUN
// #define DEBUG

//! ------------------------------------------------------------------------------

//! #define <Macros>  
//! Error Codes
//! Error Tables

BYTE ErrorTableAddress[] PROGMEM =     {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A
                                        0x0B, 0x0C};

//! ------------------------------------------------------------------------------

//! Error Macros

#define DATA_HEADER                    '@'
#define DATA_TAIL                      '+'
#define DATA_DELIMETER                 ','

#define SleepState                     SLEEP_MODE_PWR_DOWN

#define MAX_ERROR_BEFORE_EXIT          10

#define SERIAL_BAUD                    115200


#define CommsUpAddress                 1
#define DigiPinsUpAddress              2
#define AnalogSensorsUpAddress         3
#define DigitalSensorsUpAddress        4
#define BootUpAddress                  5
#define LEDPinsUpAddress               6
#define AnalogCheckedAddress           7
#define DigitalCheckedAddress          8   
#define LEDCheckedAddress              9
#define SensorsCheckedAddress          10    

#define INTAddress                     11
#define RunTimeAddress                 12

// Digital Pins

#define LEDPWR                         4
#define LEDStandby                     5
#define LEDError                       6
#define LEDDEBUG                       7
#define LEDRUN                         8


// Analog Pins

#define LightPIN                       0
#define ECGPIN                         1
#define Battery5VPIN                   2
#define Battery3P3VPIN                 3
#define Battery9VPIN                   4
#define Battery18VPIN                  5
#define RespPIN                        7


//! ------------------------------------------------------------------------------

//! states:
//!  - BOOT - 1
//!  - INIT - 2 
//!  - RUN - 3 
//!  - DEBUG - 4
//!  - ERROR - 5
//!  - SLEEP - 6

//! ------------------------------------------------------------------------------

// Variable Declaration
/**
 * A function to software reset the OS
 */
void (*restart)(void)               = 0;

int State                           = 0;
int ERRORS                          = 0;

// Pin interrupt Allocation:

    // 1 - Start Stop
    // 2 - Brown Out
    // 3 - Debug Toggle

int pinInterrupt                    = 0;

double tempLight                    = 0;
double tempECG                      = 0;
double tempBat5V                    = 0;
double tempBat3P3V                  = 0;
double tempBat9V                    = 0;
double tempBat18V                   = 0;
double tempRESP                     = 0;

string buffer                       = "";

bool checkVar                       = false;
bool SensorsOK                      = false;
bool start_stop                     = false;
bool debug                          = false;


void setup(void){
    
    togglePWRLED();
    
    changeState();
    
    toggleStandbyLED();
    toggleOff();
    togglePWRLED();
    
    attach_Start_Stop_INT();
    attach_Debug_INT();
    attach_Brownout_INT();
    
    changeState();
    
    initComms();
    initLEDPins();
    checkAnalogSensors();
    checkLEDPins();

    changeState();
    
    toggleRunLED();
}

void loop(void){
    
    wait_For_Request();

    if (debug){
        toggleDEBUGLED();
        sendRandomData();
    }
    if(!debug){
        checkERRORCount();
        Read_Analog_Sensors(buffer);
        fill_Buffer(buffer);
        send_Data(buffer);
    }
}

void checkERRORCount(int ERRORS){
    
    if(ERRORS >= ERRORCount){
        toggleErrorLED();
        sendError(false, RunTimeAddress);
        changeState(5);
        while(1){};
    }
}
